//
//  NewCBCFontView.m
//  CBCReader
//
//  Created by 王欣 on 2017/12/22.
//  Copyright © 2017年 okwei. All rights reserved.
//

#import "CBCColorView.h"
#import "CircleView.h"
#import "CommonFuctions.h"

#if __has_include(<Masonry/Masonry.h>)
#import <Masonry/Masonry.h>
#else
#import "Masonry.h"
#endif
#import "CBCReaderDef.h"
#import "CBCReadConfig.h"
CGFloat width = 60;
CGFloat btnGapding = 12;
@interface CBCColorView()

@property(nonatomic,strong)UIButton * setBtn;
@property(nonatomic,strong)UIButton * color1Btn;
@property(nonatomic,strong)UIButton * color2Btn;
@property(nonatomic,strong)UIButton * color3Btn;
@property(nonatomic,strong)NSMutableArray * btns;
@end
@implementation CBCColorView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }return self;
}

-(void)setupUI{
    [self.btns addObject:self.setBtn];
    [self.btns addObject:self.color1Btn];
    [self.btns addObject:self.color2Btn];
    [self.btns addObject:self.color3Btn];
    int i = 0;
    for (UIButton * btn in self.btns) {
        [self addSubview:btn];
        if (i!=0) {
            
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:(UIControlEventTouchUpInside)];
        }
        i++;
    }
    [self addSubview:self.setBtn];
    [self addSubview:self.color1Btn];
    [self addSubview:self.color2Btn];
    [self addSubview:self.color3Btn];
    
    
    
    [self.btns  mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:width leadSpacing:MenuPadding tailSpacing:MenuPadding];
    
    [self.btns mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(width, width));
    }];
    NSString * imgName = [CBCReadConfig shareInstance].themeImageName ;
    
    if ([imgName isEqualToString:@"read_bg_y.jpg"]) {
        self.color1Btn.selected = YES;
        self.color2Btn.selected = NO;
        self.color3Btn.selected = NO;
    }else if ([imgName isEqualToString:@"read_bg_w.jpg"]){
        self.color1Btn.selected = NO;
        self.color2Btn.selected = NO;
        self.color3Btn.selected = YES;
    }else{
        self.color1Btn.selected = NO;
        self.color2Btn.selected = YES;
        self.color3Btn.selected = NO;
    }
    
}


-(void)btnClick:(UIButton*)btn{
    for (UIButton * button in _btns) {
        if (button == btn) {
            button.selected = YES;
        }else{
            button.selected = NO;
        }
        
    }
    NSString * imageName = [NSString new];
    if (btn==self.color3Btn) {
        imageName = @"read_bg_w.jpg";
    }else if (btn == self.color2Btn){
        imageName = @"read_bg_g.jpg";
    }else{
        imageName = @"read_bg_y.jpg";
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:CBCThemeNotification object:imageName];
}

-(UIButton *)setBtn{
    if (!_setBtn) {
        _setBtn = [[UIButton alloc]init];
        [_setBtn setTitle:@"背景" forState:(UIControlStateNormal)];
        [_setBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    }return _setBtn;
}
-(UIButton*)color1Btn{
    if (!_color1Btn) {
        _color1Btn = [[UIButton alloc]init];
        UIImage * image = [CommonFuctions cbcReaderImage:@"backGroundOne"];
        UIImage * selImage = [CommonFuctions cbcReaderImage:@"backGroundOneSel"];
        [_color1Btn setImage:image forState:(UIControlStateNormal)];
        [_color1Btn setImage:selImage forState:(UIControlStateHighlighted)];
        [_color1Btn setImage:selImage forState:(UIControlStateSelected)];
    }return _color1Btn;
}
-(UIButton*)color2Btn{
    if (!_color2Btn) {
        _color2Btn = [[UIButton alloc]init];
        UIImage * image = [CommonFuctions cbcReaderImage:@"backGroundTwo"];
        UIImage * selImage = [CommonFuctions cbcReaderImage:@"backGroundTwoSel"];
        [_color2Btn setImage:image forState:(UIControlStateNormal)];
        [_color2Btn setImage:selImage forState:(UIControlStateHighlighted)];
        [_color2Btn setImage:selImage forState:(UIControlStateSelected)];
    }return _color2Btn;
}
-(UIButton*)color3Btn{
    if (!_color3Btn) {
        _color3Btn = [[UIButton alloc]init];
        UIImage * image = [CommonFuctions cbcReaderImage:@"backGroundThree"];
        UIImage * selImage = [CommonFuctions cbcReaderImage:@"backGroundThreeSel"];
        [_color3Btn setImage:image forState:(UIControlStateNormal)];
        [_color3Btn setImage:selImage forState:(UIControlStateHighlighted)];
        [_color3Btn setImage:selImage forState:(UIControlStateSelected)];

    }return _color3Btn;
}


-(NSMutableArray *)btns{
    if (!_btns) {
        _btns = [NSMutableArray array];
    }return _btns;
}
@end
